#!/usr/bin/env bash

############################## 脚本列表 ##############################
author_list="X1a0He X1a0He cui521 longzhuzhu JDDJ didi ks"
#####################################################################

############################## 维护:lxrcbw ##############################
# 仓库地址:https://gitee.com/lxrcbw/jd_shell_nevinee_lxrcbw
# 感谢各位大佬的脚本
#########################################################################

# X1a0He大佬的修复试用脚本
scripts_base_url_1=https://lxrcbw.coding.net/p/lxrcbw/d/jd_scripts_fixed_X1a0He/git/raw/main/
my_scripts_list_1="jd_try_xh.js"

# X1a0He大佬的清空购物车脚本
scripts_base_url_2=https://lxrcbw.coding.net/p/lxrcbw/d/jd_scripts_fixed_X1a0He/git/raw/main/
my_scripts_list_2="jd_cart_remove.js"

# cui521大佬的店铺签到脚本
scripts_base_url_3=https://gitee.com/lxrcbw/JDsc/raw/master/
my_scripts_list_3="jd_dpqd.js jd_dpqd2.js"

# 龙猪猪大佬的京豆雨脚本
scripts_base_url_4=https://lxrcbw.coding.net/p/lxrcbw/d/lxrcbw/git/raw/master/
my_scripts_list_4="long_half_redrain.js long_super_redrain.js"

# passerby-b大佬的京东到家脚本
scripts_base_url_5=https://lxrcbw.coding.net/p/lxrcbw/d/JDDJ/git/raw/main/
my_scripts_list_5="jddj_bean.js jddj_fruit.js jddj_fruit_collectWater.js jddj_getPoints.js jddj_plantBeans.js"

# passerby-b大佬的滴滴橙心果园脚本
scripts_base_url_6=https://lxrcbw.coding.net/p/lxrcbw/d/didi_fruit/git/raw/main/
my_scripts_list_6="dd_fruit.js"

# passerby-b大佬的快手果园脚本
scripts_base_url_7=https://lxrcbw.coding.net/p/lxrcbw/d/ks_fruit/git/raw/main/
my_scripts_list_7="ks_fruit.js"


############################ 是否强制替换脚本的定时 ############################
# 设为"true"时强制替换脚本的定时，设为"false"则不替换脚本的定时...
Enablerenew="false"

############################## 随机函数 ##############################
rand(){
    min=$1
    max=$(($2-$min+1))
    num=$(cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' '{print $1}')
    echo $(($num%$max+$min))
}

############################## 手动删除失效脚本 ##############################
cd $ScriptsDir
# rm -rf lxrcbw_jd_try.js

############################## 开始下载脚本 ##############################
index=1
for author in $author_list
do
  echo -e "######################### 开始下载 $author 的脚本 #########################"
  # 下载my_scripts_list中的每个js文件，重命名增加前缀"作者昵称_"，增加后缀".new"
  eval scripts_list=\$my_scripts_list_${index}
  eval url_list=\$scripts_base_url_${index}
  for js in $scripts_list
  do
    eval url=$url_list$js
    eval name=$author"_"$js
    echo $name
    wget -q --no-check-certificate $url -O $name.new

    # 如果上一步下载没问题，才去掉后缀".new"，如果上一步下载有问题，就保留之前正常下载的版本
    if [ $? -eq 0 ]; then
      mv -f $name.new $name
      echo -e "$name 更新成功!!!"
	  croname=`echo "$name"|awk -F\. '{print $1}'`
	  script_date=`cat  $name|grep "http"|awk '{if($1~/^[0-59]/) print $1,$2,$3,$4,$5}'|sort |uniq|head -n 1`
	  [ -z "${script_date}" ] && script_date=`cat  $name|grep -Eo "([0-9]+|\*) ([0-9]+|\*) ([0-9]+|\*) ([0-9]+|\*) ([0-9]+|\*)"|sort |uniq|head -n 1`
	  if [ -z "${script_date}" ]; then
	    cron_min=$(rand 1 59)
	    cron_hour=$(rand 7 9)
      [ $(grep -c "$croname" ${ConfigDir}/crontab.list) -eq 0 ] && sed -i "/hangup/a${cron_min} ${cron_hour} * * * bash jd $croname"  ${ConfigDir}/crontab.list
	  else
	    check_existing_cron=`grep -c "$croname" ${ConfigDir}/crontab.list`
	    echo $name "开始添加定时..."
	    if [ "${check_existing_cron}" -eq 0 ]; then
	      sed -i "/hangup/a${script_date} bash ${ShellDir}/jd.sh $croname"  ${ConfigDir}/crontab.list
	      echo -e "$name 成功添加定时!!!\n"
	    else
	      if [ "${Enablerenew}" = "true" ]; then
	      	echo -e "检测到"$name"定时已存在开始替换...\n"
	        grep -v "$croname" ${ConfigDir}/crontab.list > output.txt
		      mv -f output.txt ${ConfigDir}/crontab.list
		      sed -i "/hangup/a${script_date} bash ${ShellDir}/jd.sh $croname"  ${ConfigDir}/crontab.list
	        echo -e "替换"$name"定时成功!!!"
	      else
	        echo -e "$name 存在定时,已选择不替换...\n"
	      fi
	    fi
	  fi
    else
      [ -f $name.new ] && rm -f $name.new
      echo -e "$name 脚本失效,已删除脚本...\n"
      croname=`echo "$name"|awk -F\. '{print $1}'`
      check_existing_cron=`grep -c "$croname" ${ConfigDir}/crontab.list`
      if [ "${check_existing_cron}" -ne 0 ]; then
        grep -v "$croname" ${ConfigDir}/crontab.list > output.txt
        mv -f output.txt ${ConfigDir}/crontab.list
        echo -e "检测到"$name"残留文件..."
        rm -f ${name:-default}
        echo -e "开始清理"$name"残留文件..."
        cd $LogDir
        rm -rf ${croname:-default}
        echo -e "清理"$name"残留文件完成!!!\n"
        cd $ScriptsDir
      fi
    fi
  done
  index=$[$index+1]
done


############################## 更新diy.sh ##############################
cd $ConfigDir
echo -e "开始更新 diy.sh "
wget -q --no-check-certificate https://lxrcbw.coding.net/p/lxrcbw/d/lxrcbw/git/raw/master/diy.sh -O diy.sh.new
if [ $? -eq 0 ]; then
  mv -f diy.sh.new diy.sh
  echo -e "更新 diy.sh 成功!!!"
else
  rm -rf diy.sh.new
  echo -e "更新 diy.sh 失败...\n"
fi
